package com.ph.irp.base.verifyproject.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.verifyproject.service.PBaseVerifyprojectStorageService;
import com.ph.irp.dto.PageDTO;
import com.ph.irp.dto.StorageDTO;
import com.ph.irp.model.PBaseVerifyprojectStorage;
import com.ph.util.pagination.PageUtil;
import com.ph.util.pagination.ShowPageList;

public class VerifyprojectStorageAction extends ActionSupport implements
		ShowPageList {
	private PageDTO page;
	private String cpage;
	private PBaseVerifyprojectStorageService storageService;
	private String rowIndex;

	public String getRowIndex() {
		return rowIndex;
	}

	public void setRowIndex(String rowIndex) {
		this.rowIndex = rowIndex;
	}

	public void setStorageService(
			PBaseVerifyprojectStorageService storageService) {
		this.storageService = storageService;
	}

	private StorageDTO sdto = null;
	private List<StorageDTO> sdtos = null;
	private List<StorageDTO> tempsdtos;
	private PBaseVerifyprojectStorage verifyprojectStorage = null;
	private Object[] rowlist = null;

	public PageDTO getPage() {
		return page;
	}

	public void setPage(PageDTO page) {
		this.page = page;
	}

	public String getCpage() {
		return cpage;
	}

	public void setCpage(String cpage) {
		this.cpage = cpage;
	}

	public StorageDTO getSdto() {
		return sdto;
	}

	public void setSdto(StorageDTO sdto) {
		this.sdto = sdto;
	}

	public List<StorageDTO> getSdtos() {
		return sdtos;
	}

	public void setSdtos(List<StorageDTO> sdtos) {
		this.sdtos = sdtos;
	}

	public PBaseVerifyprojectStorage getVerifyprojectStorage() {
		return verifyprojectStorage;
	}

	public void setVerifyprojectStorage(
			PBaseVerifyprojectStorage verifyprojectStorage) {
		this.verifyprojectStorage = verifyprojectStorage;
	}

	public Object[] getRowlist() {
		return rowlist;
	}

	public void setRowlist(Object[] rowlist) {
		this.rowlist = rowlist;
	}

	public String showStorageList() throws Throwable {
		tempsdtos = storageService
				.getStorageList(new PBaseVerifyprojectStorage());

		page = PageUtil.generatePageDTO(tempsdtos,
				"showStoragePageList.action", cpage);
		sdtos = page.getResList();
		verifyprojectStorage = null;
		sdto = null;
		return "success";
	}

	public String showPageList() throws Throwable {
		if (tempsdtos != null) {
			page = PageUtil.generatePageDTO(tempsdtos,
					"showStoragePageList.action", cpage);
			sdtos = page.getResList();
			
			
			if(rowIndex!=null){
				return "select";
			}else{
				return "success";
			}
			
		} else {
			return "input";
		}

	}

	public String getStorageByCondition() throws Throwable {
		String name = verifyprojectStorage.getStorageName().trim();
		String code = verifyprojectStorage.getStorageCode().trim();
		String address = verifyprojectStorage.getStorageAddress().trim();
		verifyprojectStorage.setStorageName(name);
		verifyprojectStorage.setStorageCode(code);
		verifyprojectStorage.setStorageAddress(address);
		tempsdtos = storageService.getStorageList(verifyprojectStorage);
		page = PageUtil.generatePageDTO(tempsdtos,
				"showStoragePageList.action", cpage);
		sdtos = page.getResList();

		verifyprojectStorage = null;
		return "success";
	}

	public String selectStorage() throws Throwable {
		rowIndex = ServletActionContext.getRequest().getParameter("rowIndex");
		if (verifyprojectStorage != null) {
			String name = verifyprojectStorage.getStorageName().trim();
			String code = verifyprojectStorage.getStorageCode().trim();
			String address = verifyprojectStorage.getStorageAddress().trim();
			verifyprojectStorage.setStorageName(name);
			verifyprojectStorage.setStorageCode(code);
			verifyprojectStorage.setStorageAddress(address);
			tempsdtos = storageService.getStorageList(verifyprojectStorage);
			verifyprojectStorage = null;
		} else {
			tempsdtos = storageService
					.getStorageList(new PBaseVerifyprojectStorage());
		}

		page = PageUtil.generatePageDTO(tempsdtos,
				"showStoragePageList.action", cpage);
		sdtos = page.getResList();
		return "success";
	}

	// List页面跳转到update修改页面
	public String showEditStorage() throws Throwable {
		Integer storageId = Integer.valueOf((String) rowlist[0]);
		sdto = storageService.getStorageById(storageId);
		return "success";
	}

	public String editStorage() throws Throwable {
		storageService.updateStorage(sdto.getStorage());
		sdto = null;
		verifyprojectStorage = null;
		return "success";
	}

	public String addStorage() throws Throwable {
		storageService.addStorage(sdto.getStorage());

		sdto = null;
		return "success";
	}

	public String showAddStorage() throws Throwable {
		sdto = null;
		return "success";
	}

	public String deleteStorage() throws NumberFormatException, Throwable {
		for (int i = 0; i < rowlist.length; i++) {
			storageService.deleteStorage(Integer.valueOf((String) rowlist[i]));
		}
		return "success";
	}
}
